<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Catalog extends CI_Controller {
	public $page_Name='catalog';
	public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('is_login')){
			redirect('/admin/auth/login');
		}
		$this->load->model('Catalog_model');
	}
	public function index(){
    	$this->load->model('Catalog_model');
		$catalogs=$this->Catalog_model->get_all();
		$data=array(
			'catalogs'=>$catalogs,
			'pageName'=>'catalog'
		);
		$this->load->view('admin/admin_catalog',$data);
    }
	public function add(){
		$this->load->library('form_validation');
		//$this->form_validation->set_rules('id', 'id', 'required|integer');
	    $this->form_validation->set_rules('name', '标题', 'required');
	    $catalog=new stdClass();
	    $catalog->id="";
	    $catalog->name="";
		if ($this->form_validation->run() == FALSE){
	    	$data=array(
	    		'catalog'=>$catalog,
	    		'a'=>'添加'
	    		);
	    	$this->load->view('admin/catalog_form',$data);
	    }else{
	    	$data=$this->input->post();
	    	$this->Catalog_model->add($data);
			redirect("/admin/Catalog/index");
	    }
	}
	public function edit($id){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 'id', 'required');
	    $this->form_validation->set_rules('name', '类型', 'required');
		if ($this->form_validation->run() == FALSE){
	    	$data=array(
	    		'catalog'=>$this->Catalog_model->get_one($id),
	    		'a'=>'编辑',
	    		);
	    	$this->load->view('admin/catalog_form',$data);
	    }else{
	    	$data=$this->input->post();
	    	$this->Catalog_model->edit($data,$id);
			redirect("/admin/Catalog/index");
	    }
	}
   	public function del($id){
   		$this->Catalog_model->del($id);
   		redirect("/admin/Catalog/index");
   	}
}
